﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.OleDb;
using System.Data;
using System.Configuration;

/// <summary>
/// Summary description for ComputerDBConnection
/// </summary>
public class ComputerDBConnection
{
    OleDbConnection conn;
    OleDbCommand cmd;
    OleDbDataAdapter datatAdapter;
    OleDbDataReader dataReader;
    public String connString = "";

	public ComputerDBConnection()
    {
        conn = new OleDbConnection();
        conn.ConnectionString = ConfigurationManager.ConnectionStrings["AssetDbConnection"].ConnectionString;
	}

    public Computer getComputerByID(int computerID)
    {
        connString = "SELECT * FROM Computers WHERE Computer_ID = @Computer_ID";
        cmd = conn.CreateCommand();
        cmd.CommandText = connString;

        conn.Open();

        OleDbParameter computerIDParam = new OleDbParameter();
        computerIDParam.ParameterName = "@Computer";
        computerIDParam.OleDbType = OleDbType.Numeric;
        computerIDParam.Size = 255;
        computerIDParam.Direction = ParameterDirection.Input;
        computerIDParam.Value = computerID;

        cmd.Parameters.Add(computerIDParam);

        //myOleDbCommand.Connection.Open();
        OleDbDataReader dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

        Computer computer = new Computer();
        while (dataReader.Read())
        {
            computer.hardware_ID = Int32.Parse(dataReader["Hardware_ID"].ToString());
            computer.computer_ID = Int32.Parse(dataReader["Computer_ID"].ToString());
            computer.license = dataReader["License"].ToString();
            computer.computer_Name = dataReader["Computer_Name"].ToString();
        }

        closeObjects();

        return computer;
    }

    public DataSet getComputerDataSet_ByAdvStrSearch(String searchBy, String assetID)
    {
        DataSet ds = new DataSet();

        cmd = conn.CreateCommand();

        conn.Open();
        OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM Computers WHERE " + searchBy + " = '" + assetID + "';", conn);
        da.Fill(ds, "ComputerTable");

        closeObjects();

        return ds;
    }

    public DataSet getComputerDataSet()
    {
        DataSet ds = new DataSet();

        cmd = conn.CreateCommand();

        conn.Open();
        OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM Computers;", conn);
        da.Fill(ds, "ComputerTable");

        closeObjects();

        return ds;
    }

    public DataSet getComputerDataSet(int computerID)
    {
        DataSet ds = new DataSet();

        cmd = conn.CreateCommand();

        conn.Open();
        OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM Computers WHERE Computer_ID = " + computerID + ";", conn);
        da.Fill(ds, "ComputerTable");

        closeObjects();

        return ds;
    }

    public void updateAll(Computer computer)
    {
        String connStringUpdate = "UPDATE Computers SET Computers.License = " + computer.license + ", Computers.Computer_Name = " + computer.computer_Name + "WHERE Computers.Computer_ID = "+computer.computer_ID+";";

        cmd = new OleDbCommand(connStringUpdate, conn);
        conn.Open();

        cmd.ExecuteNonQuery();
        closeObjects();
    }

    public void update(Computer computer)
    {
        String connStringUpdate = "UPDATE Computers SET Computer_Name = '"+computer.computer_Name+"' WHERE Computer_ID = "+computer.computer_ID+";";

        cmd = new OleDbCommand(connStringUpdate, conn);
        conn.Open();

        cmd.ExecuteNonQuery();
        closeObjects();
    }


    public void delete(Computer computer)
    {
        //OleDbCommand cmd;
        //OleDbConnection conn = new OleDbConnection();
        connString = "DELETE FROM Computers WHERE Computers_ID = " + computer.computer_ID + ";";
        conn.ConnectionString = ConfigurationManager.ConnectionStrings["AssetDbConnection"].ConnectionString;
        
        cmd = new OleDbCommand(connString, conn);
        conn.Open();

        cmd.ExecuteNonQuery();
        closeObjects();
    }

    public void closeObjects()
    {
        if (conn.State.ToString().Equals("Open"))
        {
            conn.Close();
        }
        if (cmd.Connection.State.ToString().Equals("Open"))
        {
            cmd.Connection.Close();
        }
    }

}